﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SMS.Web.Models;
using Wicresoft.SMS.Data;
using Wicresoft.SMS.Common;

namespace Wicresoft.SMS.Web.Controllers
{
    public class ReportController : CustomBaseController
    {
        //
        // GET: /Report/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Overview()
        {
            OverViewReportModel model = new OverViewReportModel();
            WebConfigProvider configProvider = new WebConfigProvider();
            model.StartDate = configProvider.SystemStartDate;
            model.TotalRunTime = (DateTime.Now - Convert.ToDateTime(configProvider.SystemStartDate)).ToString();
            IReportWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            model.TotalMessage = dataAccess.GetTotalMessage(Convert.ToDateTime(configProvider.SystemStartDate));
            model.TotalMoney = model.TotalMessage * Convert.ToDouble(configProvider.Price);
            return View(model);
        }


        public ActionResult History(HistoryReportModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IReportWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            if (String.IsNullOrEmpty(Request.QueryString["starttime"]) || String.IsNullOrEmpty(Request.QueryString["endtime"]))
            {
                Model.StartTime = DateTime.Today.AddDays(-7);
                Model.EndTime = DateTime.Today;
                Model.Frequence = queryFrequence.day;
            }
            SelectList ListItems = new SelectList(SelectFrequenc.SelectItems().AsEnumerable(), "Value", "Text");
            ViewData["ListItems"] = ListItems;
            List<HistoryReport> report = dataAccess.QueryHistoryReport(Model.StartTime, Model.EndTime.AddDays(1).AddMinutes(-1), Model.Frequence);

            System.Web.UI.DataVisualization.Charting.SeriesChartType chartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line;
            Model.ImageSource = ReportHelper.GetImageRoute(WebReport.ChageReportModel(report), chartType, Model.StartTime, Model.EndTime, null);

            return View(Model);
        }

        public ActionResult Group(GroupReportModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IReportWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            if (String.IsNullOrEmpty(Request.QueryString["starttime"]) || String.IsNullOrEmpty(Request.QueryString["endtime"]))
            {
                Model.StartTime = DateTime.Today.AddDays(-7);
                Model.EndTime = DateTime.Today;
                Model.MaxShow = 10;
            }
            SelectList ListItems = new SelectList(SelectFrequenc.MaxShowItems().AsEnumerable(), "Value", "Text");
            ViewData["MaxItems"] = ListItems;
            List<GroupReport> report = dataAccess.QueryGroupReport(Model.StartTime, Model.EndTime.AddDays(1), Model.MaxShow);
            
            System.Web.UI.DataVisualization.Charting.SeriesChartType chartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
            Model.ImageSource = ReportHelper.GetImageRoute(WebReport.ChageReportModel(report), chartType, Model.StartTime, Model.EndTime, "GroupReport");
            return View(Model);
        }

        public ActionResult User(UserReportModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IReportWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            if (String.IsNullOrEmpty(Request.QueryString["starttime"]) || String.IsNullOrEmpty(Request.QueryString["endtime"]))
            {
                Model.StartTime = DateTime.Today.AddDays(-7);
                Model.EndTime = DateTime.Today;
                Model.MaxShow = 10;
            }
            SelectList ListItems = new SelectList(SelectFrequenc.MaxShowItems().AsEnumerable(), "Value", "Text");
            ViewData["MaxItems"] = ListItems;
            List<IndividualReprot> report = dataAccess.QueryIndividualReprot(Model.GroupId, Model.StartTime.AddDays(1), Model.EndTime, Model.MaxShow);
            string GroupName = string.Empty;
            if (Model.GroupId != 0)
                GroupName = dataAccess.GetDepartnameById(Model.GroupId);
            else
                GroupName = "0";
            System.Web.UI.DataVisualization.Charting.SeriesChartType chartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
            Model.ImageSource = ReportHelper.GetImageRoute(WebReport.ChageReportModel(report), chartType, Model.StartTime, Model.EndTime, GroupName);
            return View(Model);
        }


        
    }
}
